package exercise17;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.border.TitledBorder;

import chapter15.MessagePanel;

public class Exercise17_1 extends JFrame{

	protected MessagePanel messagePanel = new MessagePanel("Welcome to Java");
	
	private JButton jbtLeft = new JButton("<=");
	private JButton jbtRight = new JButton("=>");
	
	//添加一组选择背景的单选按钮
	private JRadioButton jrbRed;
	private JRadioButton jrbYellow;
	private JRadioButton jrbWhite;
	private JRadioButton jrbGray;
	private JRadioButton jrbGreen;
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Exercise17_1 frame = new Exercise17_1();
		frame.setTitle("Exercise17_1");
//		frame.setSize(250, 100);
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		
	}
	
	public Exercise17_1(){
		messagePanel.setBackground(Color.WHITE);
		
		JPanel jpRadioBtns = new JPanel();
		jpRadioBtns.setBorder(new TitledBorder("Select Message Panel Background"));
		jpRadioBtns.add(jrbRed = new JRadioButton("Red"));
		jpRadioBtns.add(jrbYellow = new JRadioButton("Yellow"));
		jpRadioBtns.add(jrbWhite = new JRadioButton("White"));
		jpRadioBtns.add(jrbGray = new JRadioButton("Gray"));
		jpRadioBtns.add(jrbGreen = new JRadioButton("Green"));
		
		ButtonGroup group = new ButtonGroup();
		group.add(jrbRed);
		group.add(jrbYellow);
		group.add(jrbWhite);
		group.add(jrbGray);
		group.add(jrbGreen);
		
		JPanel jpButtons = new JPanel();
		jpButtons.add(jbtLeft);
		jpButtons.add(jbtRight);
		
		jbtLeft.setMnemonic('L');
		jbtRight.setMnemonic('R');
		
		jbtLeft.setToolTipText("Move message to left");
		jbtRight.setToolTipText("Move message to right");
		
		setLayout(new BorderLayout());
		add(jpRadioBtns, BorderLayout.NORTH);
		add(messagePanel, BorderLayout.CENTER);
		add(jpButtons, BorderLayout.SOUTH);
		
		jbtLeft.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				messagePanel.moveLeft();
			}
		});
		
		jbtRight.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				messagePanel.moveRight();
			}
		});
		
		jrbRed.addItemListener(new ItemListener() {
			
			
			public void itemStateChanged(ItemEvent e) {
				// TODO Auto-generated method stub
				messagePanel.setBackground(Color.RED);
			}
		});
		
		jrbYellow.addItemListener(new ItemListener() {
			
			
			public void itemStateChanged(ItemEvent e) {
				// TODO Auto-generated method stub
				messagePanel.setBackground(Color.YELLOW);
			}
		});
		
		jrbWhite.addItemListener(new ItemListener() {
			
			
			public void itemStateChanged(ItemEvent e) {
				// TODO Auto-generated method stub
				messagePanel.setBackground(Color.WHITE);
			}
		});
		
		jrbGray.addItemListener(new ItemListener() {
			
			
			public void itemStateChanged(ItemEvent e) {
				// TODO Auto-generated method stub
				messagePanel.setBackground(Color.GRAY);
			}
		});
		
		jrbGreen.addItemListener(new ItemListener() {
			
			
			public void itemStateChanged(ItemEvent e) {
				// TODO Auto-generated method stub
				messagePanel.setBackground(Color.GREEN);
			}
		});
	}

}